<template>
    <div class="outer">
        <div class="y1">
            <div>
                <div class="list" v-for="(item, index) in list" :key="item.id">
                    <lExcerpt v-if="(index + 1) % 2 != 0" :maxWidth="400" :list="item" @reList="getExpId"></lExcerpt>
                </div>
            </div>
            <div>
                <div class="list" v-for="(item, index) in list" :key="item.id">
                    <lExcerpt v-if="(index + 1) % 2 == 0" :maxWidth="400" :list="item" @reList="getExpId"></lExcerpt>
                </div>
            </div>
            <el-empty style="margin:0 auto" description="没有内容了" v-if="list.length == 0"></el-empty>

        </div>


    </div>
</template>
<script>
import lExcerpt from "./l-excerpt"
export default {
    components: {
        lExcerpt
    },
    data() {
        return {
            list: [],
            dialogVisible: false,
            form: { type: '' },
            pageNum: 1,
            totalPage: 1,
            loading: false,
            user_id: 0
        }
    },
    computed: {
        keyword() {
            return this.$route.query.keyword
        },
        id() {
            return this.$route.query.id
        }
    },

    watch: {
        keyword(n) {
            this.pageNum = 1
            this.getList()
        },
    },
    mounted() {
        let uid = this.$route.query.id
        if (uid) {
            this.user_id = uid
            this.pageNum = 1
        }
        this.getList()
        window.addEventListener('scroll', this.handleScroll); // 监听滚动事件

    },
    beforeDestroy() {
        window.removeEventListener('scroll', this.handleScroll); // 移除监听
    },

    methods: {
        getExpId(info) {
            let list = this.list
            let id = info.id
            let op = info.op
            let status = info.status
            list.forEach((i, d) => {
                if (id == i.id) {
                    if (op == 2) {
                        this.list[d].is_like = status
                        // 点赞
                        if (status == 1) {
                            this.list[d].likes += 1
                        } else {
                            this.list[d].likes -= 1
                        }
                    } else {
                        // 展开
                        this.list[d].views += 1
                    }
                }
            })
        },
        showCol(id) {
            let params = {
                id
            }
            this.$api.showCol(params).then(res => {
                if (res.data.code == 1) {
                    this.getList()
                }
            })
        },

        getList() {
            this.loading = true
            let params = {
                page: this.pageNum,
                user_id: this.user_id,
                keyword: this.keyword
            }
            this.$api.getUexp(params).then(res => {
                if (res.data.code == 1) {
                    if (this.keyword) {
                        this.list = []
                    }
                    this.totalPage = res.data.data.last_page
                    this.list = this.list.concat(res.data.data.data);
                    this.total = res.data.data.total
                    this.loading = false
                }

            })
        },
        load() {
            console.log("处理触底");
            var page = this.pageNum
            //如果请求未结束就返回不连续请求
            if (this.loading) {
                return
            }
            //数据请求完成无更多数据时
            if (page >= this.totalPage) {
                return false;
            }
            page = parseInt(page) + 1
            this.pageNum = page
            this.getList()
        },
        onsubmit() {
            console.log(this.form);

        },
        handleScroll() {
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            const windowHeight = window.innerHeight;
            const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
            // 判断是否滚动到底部
            if (scrollTop + windowHeight >= scrollHeight - 20) {
                this.load()
            }
        },
    }
}
</script>
<style lang="scss" scoped>
.outer {
    display: flex;
    align-items: flex-start;
    padding-bottom: 20px;

    .y1 {
        width: 100%;
        display: flex;
        flex-wrap: wrap;

        >div {
            width: calc(50% - 12.5px);

            &:nth-child(2n) {
                margin-left: 25px;
            }
        }
    }

    .list {
        display: flex;
        align-items: flex-start;
        flex-wrap: wrap;
        width: 100%;

        .title {
            display: flex;
            align-items: center;
            font-weight: 400;
            font-size: 18px;
            color: #000000;

            img {
                width: 16px;
                height: auto;
                margin-right: 16px;
            }

            .name {
                width: 700px;
            }
        }

        .set {
            display: flex;
            align-items: center;
            justify-content: space-between;
            flex: 1;

            .data {
                font-size: 16px;
                color: #999;
                display: flex;
                align-items: center;

                >span {
                    display: inline-block;
                    flex-shrink: 0;
                }

                .dz {
                    margin-right: 15px;
                    width: 100px;
                    text-align: center;
                }

                .line {
                    width: 2px;
                    height: 15px;
                    background-color: #999;
                }

                .nums {
                    width: 120px;
                    text-align: center;
                }

                .nickname {
                    margin-left: 15px;
                }
            }

            .btn {
                display: flex;

                .price {
                    font-size: 20px;
                    color: #D31B23;
                    margin-right: 50px;
                }

                button {
                    width: 80px;
                    height: 30px;
                    background-color: #1169E5;
                    border: none;
                    border-radius: 5px;
                    color: #fff;
                }
            }
        }
    }
}
@media screen and (max-width:768px) {
    .outer .y1 {
        width: 100vw;
        padding: 0 10px;
        > div{
        width: 100%;
    }
    }
}
</style>